package com.zhsj.OkeHomeConsumption.utils;

import android.annotation.SuppressLint;

import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.other.MD5Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;


/**
 * @ClassName: SignUtil
 * @Description: TODO 生成签名工具类
 * @author zhangpeisen
 * @date 2016年9月18日 下午6:29:53
 */
public class SignUtil {
	private static SignUtil instance = null;

	private SignUtil() {
	};

	public static SignUtil getInstance() {
		if (instance == null) {
			instance = new SignUtil();
		}
		return instance;
	}
	/**
	 * @Title: getSign @Description: TODO @param @param token @param @param
	 * v @param @param t @param @param deviceId @param @param
	 * platform @param @return @return String 返回类型 @throws
	 */
	@SuppressLint("DefaultLocale")
	public String getSign(HashMap<String, String> params) {
		StringBuilder stringBuilder = new StringBuilder();
		List<String> sortParam = getNameSort(params);
		for (String key : sortParam) {
			stringBuilder.append(key).append("=").append(params.get(key)).append("&");
		}
		stringBuilder.append("token=").append(Constants.TOKEN);
		return MD5Utils.MD5To32(stringBuilder.toString()).toUpperCase();
	}

	/**
	 * @Title: getNameSort
	 * @Description: 获取排序的maps 
	 * TODO @param
	 * @return void 返回类型 @throws
	 */
	@SuppressLint("DefaultLocale")
	public List<String> getNameSort(HashMap<String, String> params) {
		List<String> mEntityLists = new ArrayList<String>();
		for (String UrlParamKeys : params.keySet()) {
			mEntityLists.add(UrlParamKeys);
		}
		// 参数进行排序
		Collections.sort(mEntityLists, new StringComparator());
		return mEntityLists;
	}

	/**
	 * @ClassName: StringComparator
	 * @Description: TODO 参数排序
	 * @author zhangpeisen
	 * @date 2016年9月18日 下午6:38:36
	 */
	public class StringComparator implements Comparator<String> {

		@Override
		public int compare(String Str1, String Str2) {
			// TODO Auto-generated method stub
			return Str1.compareTo(Str2);
		}
	}

}
